package syuhei.mdatools;

import java.io.IOException;
import java.util.Date;
import java.util.List;

import javax.jdo.PersistenceManager;
import javax.servlet.http.*;

import com.google.appengine.api.users.User;
import com.google.appengine.api.users.UserService;
import com.google.appengine.api.users.UserServiceFactory;

import syuhei.mdatools.PMF;
import syuhei.mdatools.database.*;

@SuppressWarnings("serial")

public class ProjectManagerServlet extends HttpServlet {
	public void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws IOException {
        UserService userService = UserServiceFactory.getUserService();
        User user = userService.getCurrentUser();

        if (user != null) {
            resp.setContentType("text/plain");
            resp.getWriter().println("Hello, " + user.getNickname());
        } else {
            resp.sendRedirect(userService.createLoginURL(req.getRequestURI()));
        }
	}
	
	/*
	 * ユーザはファイルの内容を読み込み受信する
	 */
	public void load(String fname){
        PersistenceManager pm = PMF.get().getPersistenceManager();
        javax.jdo.Query query = pm.newQuery(Projects.class);
        query.setFilter("fname == fnameParam");
        query.declareParameters("String fnameParam");
        try {  
            List<Projects> results = (List<Projects>) query.execute(fname);
            if(results.isEmpty())
            {
            }
            else
            {
            	
            }
        } finally {  
            query.closeAll();  
            pm.close();  
        }

	}

	/*
	 * ユーザはファイルの内容を送信し保存する
	 */
	public void save(String fname){
		Date date = new Date();

		PersistenceManager pm;
		pm = PMF.get().getPersistenceManager();

		Projects project = new Projects(fname);
		
		try {
			pm.makePersistent(project);
		} finally {
			pm.close();
		}
	}
}
